package org.example.rabbitmq.direct;

import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import org.example.rabbitmq.util.ConnectionUtil;

/**
 * @author Lenovo
 * @version 1.0
 * @description TODO
 * @date 26/7/2023 上午8:45
 */
// 路由模式(定向模式)
public class Producer {
    public static final String EXCHANGE_NAME = "direct_exchange";
    public static final String QUEUE_NAME1 = "direct_queue1";
    public static final String QUEUE_NAME2 = "direct_queue2";
    public static void main(String[] args) throws Exception {
        // 创建连接
        Connection connection = ConnectionUtil.getConnection();
        // 创建信道
        Channel channel = connection.createChannel();
        // 声明交换机
        channel.exchangeDeclare(EXCHANGE_NAME, BuiltinExchangeType.DIRECT,true,false,false,null);
        // 声明队列
        channel.queueDeclare(QUEUE_NAME1,true,false,false,null);
        channel.queueDeclare(QUEUE_NAME2,true,false,false,null);
        // 将队列绑定到交换机
        channel.queueBind(QUEUE_NAME1,EXCHANGE_NAME,"error");// key 是 error
        // 多个key
        channel.queueBind(QUEUE_NAME2,EXCHANGE_NAME,"info");
        channel.queueBind(QUEUE_NAME2,EXCHANGE_NAME,"warning");
        channel.queueBind(QUEUE_NAME2,EXCHANGE_NAME,"error");

        // 发送消息
        String message = "消息";

        channel.basicPublish(EXCHANGE_NAME,"info",null,message.getBytes());

        // 关闭资源
        ConnectionUtil.closeResource(channel,connection);
    }
}
